package com.zxx.casserver.modules.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxx.casserver.modules.role.entity.RolePermissionDO;
import com.zxx.casserver.modules.role.mapper.RolePermissionMapper;
import com.zxx.casserver.modules.role.service.IRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色权限关联表 服务实现类
 * </p>
 *
 * @author zhoukai
 * @since 2020-08-03
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermissionDO> implements IRolePermissionService {

    @Override
    public Boolean removeRolePermission(Long roleId) {
        QueryWrapper<RolePermissionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<RolePermissionDO> rolePermissionList = list(queryWrapper);
        if (ObjectUtils.isEmpty(rolePermissionList)) {
            return true;
        }
        Set<Integer> permissionId = new HashSet<>();
        for (RolePermissionDO rolePermissionDO : rolePermissionList) {
            permissionId.add(rolePermissionDO.getId());
        }
        return removeByIds(permissionId);
    }

    @Override
    public Boolean saveBatchRolePermission(List<RolePermissionDO> rolePermissionList) {
        //处理初始化时间
        for (RolePermissionDO rolePermissionDO : rolePermissionList) {
            rolePermissionDO.setCreateTime(System.currentTimeMillis());
        }
        return saveBatch(rolePermissionList);
    }
}
